/*
 * AscTextReaderSimpleImpl.java
 *
 * Created on August 27, 2002, 12:32 PM
 *
 * Copyright (c) 2002 The General Electric Company
 */

package com.ge.healthcare.autosc.common.util.dataItem;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.ge.healthcare.autosc.common.exceptions.BasicException;

/**
 * A simple AutoSC text reader built using a BufferedReader.
 *
 * @author Daniel S. Horwitz, Compuware Corp.
 *
 * @version AutoSC P5P6
 */
public class ASCSimpleTextReader implements IASCTextReader
{
    /**
     * Name used to identifier the reader.
     */
    private String theReaderId = null;

    /**
     * The buffered reader which does all the work.
     */
    private BufferedReader baseReader = null;

    /**
     * Create a simple AutoSC text reader.
     *
     * @param helper Object providing the utility objects and methods.
     *
     * @param readerId Name used to identifier the reader.
     *
     * @param rawSource The raw input stream for reading the data.  It is
     *  the responsibility of the caller to make sure that this is buffered.
     */
    public ASCSimpleTextReader(String readerId, InputStream rawSource )
    {
        theReaderId = readerId;
        baseReader = new BufferedReader( new InputStreamReader( rawSource ) );
    }

    /**
     * Close the reader.
     *
     * @exception AscServiceAbortException If an error is detected closing
     *  the reader, an error is logged and then an abort exception is thrown.
     */
    public void close() throws BasicException
    {
        if ( baseReader == null )
        {
            return;
        }
        try
        {
            baseReader.close();
        }
        catch ( IOException e )
        {
        	throw new BasicException("Unable to close " + theReaderId, e);
        }
        finally
        {
            baseReader = null;
        }
    }

    /**
     * Silently close the reader.  This never throws an exception.  If
     *  an I/O error is detected closing a stream, it is not logged.
     *  Other errors may be logged at the discretion of the implementor.
     */
    public void silentlyClose()
    {
        if ( baseReader == null )
        {
            return;
        }
        try
        {
            baseReader.close();
        }
        catch ( IOException e )
        {
        }
        finally
        {
            baseReader = null;
        }
    }

    /**
     * Read a line of text.  If the text is read from an underlying reader,
     *  a line is considered to be terminated by any one of a line feed,
     *  a carriage return, or a carriage return followed immediately by
     *  a linefeed.  Text lines can also be constructed by the reader, in
     *  which case the reader defines a text line.  Typically, a text line
     *  is a record from an underlying data source.
     *
     * @return A string containing the contents of the line, not including any
     *   line-termination characters, or null if the end of the data has
     *   been reached.
     *
     * @exception AscServiceAbortException If an error is detected reading
     *  a line of text, an error is logged and then an abort exception is
     *  thrown.
     */
    public String readLine() throws BasicException
    {
    	String line = null;
        try
        {
            line = baseReader.readLine();
        }
        catch ( IOException ioe )
        {
        	throw new BasicException("Unable to read a line from " + theReaderId, ioe);
        }
        return line;
    }
}
